package com.ies.ieschool.client.module.academic.view.content;

import com.google.gwt.core.client.GWT;
import com.ies.ieschool.client.module.academic.view.data.ClassData;
import com.ies.ieschool.client.module.academic.view.data.CourseData;
import com.ies.ieschool.client.module.academic.view.dialog.CourseDialog;
import com.ies.ieschool.client.module.academic.view.dialog.StudentList;
import com.ies.ieschool.client.object.ContextAreaFactory;
import com.smartgwt.client.types.Alignment;
import com.smartgwt.client.types.Side;
import com.smartgwt.client.widgets.Canvas;
import com.smartgwt.client.widgets.Label;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.form.fields.ComboBoxItem;
import com.smartgwt.client.widgets.layout.VLayout;
import com.smartgwt.client.widgets.tab.Tab;
import com.smartgwt.client.widgets.tab.TabSet;
import com.smartgwt.client.widgets.form.fields.ComboBoxItem;
import com.smartgwt.client.widgets.form.fields.SelectItem;
import com.smartgwt.client.widgets.form.fields.events.ChangeEvent;
import com.smartgwt.client.widgets.form.fields.events.ChangeHandler;
import com.smartgwt.client.widgets.grid.ListGrid;
import com.smartgwt.client.widgets.grid.ListGridField;
import com.smartgwt.client.widgets.grid.events.RecordDoubleClickEvent;
import com.smartgwt.client.widgets.grid.events.RecordDoubleClickHandler;

public class CourseSchedule extends VLayout {

	private static final String DESCRIPTION = "CourseSchedule";
	private static final int COURSE_WIDTH = 1140;
	private static final int COURSE_HEIGHT = 580;
	ListGrid ClassList;

	public CourseSchedule() {

		this.addStyleName("contextArea");
		this.setWidth("*");
		this.setHeight(COURSE_HEIGHT + 10);

		// this.addMember(new BlueBox(null, 50, "height 50"));

		final DynamicForm form = new DynamicForm();

		/*
		 * ComboBoxItem StudentClass = new ComboBoxItem();
		 * StudentClass.setTitle("ช่วงชั้น");
		 * 
		 * StudentClass.setType("comboBox");
		 * StudentClass.setValueMap("ชั้นมัธยมต้น", "ชั้นมัธยมปลาย");
		 */
		final TabSet HighClassTab = new TabSet();  
		HighClassTab.setTabBarPosition(Side.TOP);
		HighClassTab.setTabBarThickness(25);  
		HighClassTab.setTabBarAlign(Side.LEFT);  
		HighClassTab.setWidth(COURSE_WIDTH);  
		HighClassTab.setHeight(COURSE_HEIGHT-25);
		final Tab M4 = new Tab("มัธยมต้นปีที่  4");
		final Tab M5 = new Tab("มัธยมต้นปีที่  5");
		final Tab M6 = new Tab("มัธยมต้นปีที่  6");
		
		HighClassTab.addTab(M4);
		HighClassTab.addTab(M5);
		HighClassTab.addTab(M6);
		
		HighClassTab.hide();
		
		final TabSet MiddleClassTab = new TabSet();  
		MiddleClassTab.setTabBarPosition(Side.TOP);
		MiddleClassTab.setTabBarThickness(25);  
		MiddleClassTab.setTabBarAlign(Side.LEFT);  
		MiddleClassTab.setWidth(COURSE_WIDTH);  
		MiddleClassTab.setHeight(COURSE_HEIGHT-25);
		final Tab M1 = new Tab("มัธยมต้นปีที่  1");
		final Tab M2 = new Tab("มัธยมต้นปีที่  2");
		final Tab M3 = new Tab("มัธยมต้นปีที่  3");
		
		MiddleClassTab.addTab(M1);
		MiddleClassTab.addTab(M2);
		MiddleClassTab.addTab(M3);
		
		ClassList = new ListGrid(); 
		ClassList.setWidth(COURSE_WIDTH-30);
		ClassList.setHeight(COURSE_HEIGHT); 	
		ClassList.setAutoFetchData(true);
		ClassList.setShowFilterEditor(true);
		ClassList.setFilterByCell(true);
		ClassList.setFilterOnKeypress(true);
		ClassList.setDataSource(ClassData.getInstance()); 
		
		//field ชื่อหลักสูตร
    	ListGridField lsgfClassName = new ListGridField("className","ชั้น");
    	lsgfClassName.setCellAlign(Alignment.LEFT);
    	lsgfClassName.setAlign(Alignment.CENTER);
    	//field ช่วงชั้น
    	ListGridField lsgfClassAmount = new ListGridField("classAmount","จำนวนนักเรียน");
    	lsgfClassAmount.setCellAlign(Alignment.LEFT);
    	lsgfClassAmount.setAlign(Alignment.CENTER);
    	//field เกณฑ์การวัดผล
    	ListGridField lsgfTeacher = new ListGridField("teacher","อาจารย์ประจำชั้น");
    	lsgfTeacher.setCellAlign(Alignment.LEFT);
    	lsgfTeacher.setAlign(Alignment.CENTER);
    	
    	ClassList.setFields(lsgfClassName,lsgfClassAmount,lsgfTeacher);
    	
    	M1.setPane(ClassList);
    	M2.setPane(ClassList);
    	M3.setPane(ClassList);
    	M4.setPane(ClassList);
    	M5.setPane(ClassList);
    	M6.setPane(ClassList);
    	
    	ClassList.addRecordDoubleClickHandler(OnClickClasslist);
		
		SelectItem StudentClass = new SelectItem();
		StudentClass.setName("StudentClass");
		StudentClass.setTitle("ช่วงชั้น");
		StudentClass.setValueMap("ชั้นมัธยมต้น", "ชั้นมัธยมปลาย");
		StudentClass.addChangeHandler(new ChangeHandler() {  
            public void onChange(ChangeEvent event) {  
                String selectedItem = (String) event.getValue();  
               if(selectedItem.equals("ชั้นมัธยมต้น"))
               {
            	   HighClassTab.hide();
            	   MiddleClassTab.show();
            	   
            	   
               }
               else if(selectedItem.equals("ชั้นมัธยมปลาย"))
               {
            	   MiddleClassTab.hide();
            	   HighClassTab.show();
               }
            }  
        });
		
		

		/*
		 * ComboBoxItem cbiClassRange = new ComboBoxItem();
		 * cbiClassRange.setShowTitle(false); cbiClassRange.setType("comboBox");
		 * cbiClassRange.setValueMap("ประถมต้น", "ประถมปลาย", "มัธยมต้น");
		 * 
		 * 
		 * 
		 * form.setFields(cbiClassRange);
		 */
		form.setFields(StudentClass);
		this.addMember(form);
		this.addMember(MiddleClassTab);
		this.addMember(HighClassTab);

	}
	public RecordDoubleClickHandler OnClickClasslist = new RecordDoubleClickHandler() {
        @Override
        public void onRecordDoubleClick(RecordDoubleClickEvent event) {
        	  new StudentList(event) ;
        }
	};

	public static class Factory implements ContextAreaFactory {

		private String id;

		public Canvas create() {
			CourseSchedule view = new CourseSchedule();
			id = view.getID();

			GWT.log("CourseSchedule.Factory.create()->view.getID() - " + id,
					null);
			return view;
		}

		public String getID() {
			return id;
		}

		public String getDescription() {
			return DESCRIPTION;
		}
	}

	

}
